package com.fuyun.rpc.util;

import cn.hutool.core.util.StrUtil;

import java.net.InetSocketAddress;
import java.util.Objects;

/**
 * @Author fuyun。
 * @Date 2025/8/22 11:45。
 * @Description: TODO。
 **/

public class IpUtils {

    public static String tpIpPort(InetSocketAddress inetSocketAddress) {
        if (Objects.isNull(inetSocketAddress)) {
            throw new IllegalArgumentException("inetSocketAddress can not be null");
        }

        String host = inetSocketAddress.getHostString();

        if (Objects.equals(host, "localhost")) {
            host = "127.0.0.1";
        }
        return host + StrUtil.COLON + inetSocketAddress.getPort();
    }

    public static InetSocketAddress toInetSocketAddress(String address) {
        if (Objects.isNull(address)) {
            throw new IllegalArgumentException("address can not be null");
        }
        String[] split = address.split(":");
        if (split.length != 2) {
            throw new IllegalArgumentException("address format error");
        }
        return new InetSocketAddress(split[0], Integer.parseInt(split[1]));
    }

}
